package com.qf.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.Product;
import com.qf.entity.ProductDTO;
import com.qf.listener.ProductListener;
import com.qf.service.ProductService;
import com.qf.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * --- 万物归集  不随你我 ---
 * <p>
 * 快捷短语
 * main ==  public static void main(String[] args);
 * sout ==  System.out.println();
 *
 * @author shuhan
 * @date 2025/4/27
 * @desc 商品的控制层
 */

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @PostMapping("/findAll")
    public R findAll(@RequestBody ProductDTO productDTO) {
        PageHelper.startPage(productDTO.getPageNum(),productDTO.getPageSize());
        List<Product> list = productService.findAll(productDTO);
        PageInfo<Product> pageInfo = new PageInfo<>(list);

        return R.ok(pageInfo);
    }

    @GetMapping("/deletebyId")
    public R deleteById(int id) {
        Boolean flag = productService.deleteById(id);
        if (flag) {
            return R.ok();
        }
        return R.fail();
    }

    @PostMapping("/addproduct")
    public R addProduct(@RequestBody Product product) {
        Boolean flag = productService.addProduct(product);
        return R.ok();
    }

    @PostMapping("/updateproduct")
    public R updateProduct(@RequestBody Product product) {
        Boolean flag = productService.updateProduct(product);
        return R.ok();
    }

    @PostMapping("/uploadproduct")
    public R uploadProduct(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),
                    Product.class,
                    new ProductListener(productService))
                    .sheet(0).doRead();
            return R.ok();
            } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }

    }

    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        List<Product> list = productService.findAll(null);
        // 导出.xls用这个
        // response.setContentType("application/vnd.ms-excel");
        // 导出.xlsx用这个
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("商品信息", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), Product.class).sheet("商品信息").doWrite(list);
    }
}
